import java.util.LinkedList;
import java.util.Queue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-04-13
 * Time: 19:55
 */
public class Tree {

    static class TreeNode {
        public char val;
        public TreeNode left;//左孩子
        public TreeNode right;//右孩子

        public TreeNode(char val) {
            this.val = val;
        }
    }

    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        TreeNode I = new TreeNode('I');
        TreeNode X = new TreeNode('X');
        TreeNode Z = new TreeNode('Z');
        TreeNode Y = new TreeNode('Y');
        A.left = C;
        A.right = B;
        C.left = D;
        C.right = E;
//        B.left = F;
        B.right = G;
        return A;
    }

    //前序遍历
    public void preOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }

    //中序遍历
    public void inOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }

    //后序遍历
    public void postOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val + " ");
    }

    //层序遍历
    public void levelOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode top = queue.poll();
            System.out.print(top.val + " ");
            if (top.left != null) {
                queue.offer(top.left);
            }
            if (top.right != null) {
                queue.offer(top.right);
            }
        }
    }

    //求结点个数
    public int NodeSize(TreeNode root) {
        if (root == null) {
            return 0;
        }
        return NodeSize(root.left) + NodeSize(root.right) + 1;
    }

    //求叶子结点个数
    public int getLeafNodeCount(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return 1;
        }
        return getLeafNodeCount(root.left) + getLeafNodeCount(root.right);
    }

    //第k层结点的个数
    public int getKLevelNodeCount(TreeNode root, int k) {
        if (root == null) {
            return 0;
        }
        if (k == 1) {
            return 1;
        }
        return getKLevelNodeCount(root.left, k - 1) +
                getKLevelNodeCount(root.right, k - 1);
    }

    //树的高度
    public int getHeight(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int LeftHeight = getHeight(root.left);//左子树的高度
        int RightHeight = getHeight(root.right);//右子树的高度

        return Math.max(LeftHeight, RightHeight) + 1;
    }

    //是否存在某个值
    public boolean find(TreeNode root, char key) {
        if (root == null) {
            return false;
        }
        if (root.val == key) {
            return true;
        }
        boolean flg1 = find(root.left, key);
        if (flg1) {
            return true;
        }
        boolean flg2 = find(root.right, key);
        if (flg2) {
            return true;
        }
        return false;
    }

    //是否为完全二叉树
    public boolean isCompleteTree(TreeNode root) {
        if (root == null) {
            return false;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode top = queue.poll();
            if (top == null) {
                break;
            }
            queue.offer(top.left);
            queue.offer(top.right);
        }

        //
        while (!queue.isEmpty()) {
            TreeNode tmp = queue.peek();
            if (tmp != null) {
                return false;
            } else {
                queue.poll();
            }
        }

        return true;
    }
}
